// Agent herdsman in project vacaloucaMASSim2010

/* Initial beliefs and rules */

/* Initial goals */

!search_cow.

/* Plans */

+!search_cow : not pos(_,_,_)
<- .print("waiting position...");
	.wait("+pos(_,_,_)");
	.print("got position...");
	!!search_cow.
	
+!move_target : not pos(_,_,_)
<- .print("waiting position...");
	.wait("+pos(_,_,_)");
	.print("got position...");
	!!move_target.

+!search_cow : not cow(_,_,_) & pos(X,Y,_) & actions.inside_corral(X,Y)
<- 		.print("herdsman: To preso, open(fence)!");
		.broadcast(tell,open(fence));
		.print("herdsman: Mandei broadcast open(fence)!");
		.print("herdsman: Ainda nao sei onde tem vaquinha, movendo aleatoriamente...");
		actions.random_direction(Direction);
		moveTo(Direction);
		!!search_cow;
	
+!search_cow : not cow(_,_,_) & pos(X,Y,_) & not actions.inside_corral(X,Y)
<- .print("herdsman: Ainda nao sei onde tem vaquinha, movendo aleatoriamente...");
	actions.random_direction(Direction);
	moveTo(Direction);
	!!search_cow.
	
+!search_cow : cow(_,Cx,Cy) & pos(X,Y,_)
<-  .print("herdsman: Sei onde tem vaquinha, marcando a vaquinha...");
	+target(Cx,Cy);
	.print("herdsman: Buscar vaquinha.");
	!!move_target.
	
+!move_target : pos(X,Y,_) & target(Cx,Cy) & not actions.inside_corral(Cx,Cy)
	<-  .print("Buscar vaquinha.");
	    actions.astar(X,Y,Cx,Cy,Direction,0);
	    moveTo(Direction);
	    !move_target.

+!move_target : pos(X,Y,_) & target(Cx,Cy) & actions.inside_corral(Cx,Cy)
	<-  .print("Procurar outra vaquinha.");
	    -target(Cx,Cy);
	    !!search_cow.
